//
//  GetNetworkStatues.m
//  xpkc
//
//  Created by wanmeizty on 14-8-19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "GetNetworkStatues.h"

@implementation GetNetworkStatues
+(NSString *)getNetWorkStates{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    NSString *state = [[NSString alloc]init];
    int netType = 0;
    //获取到网络返回码
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            //获取到状态栏
            netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            switch (netType) {
                case 0:
                    state = @"无网络";
                    //无网模式
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                {
                    state = @"WIFI";
                }
                    break;
                default:
                    break;
            }
        }
    }
    //根据状态选择
    return state;
}
+ (BOOL)getReachability{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    /**
     AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
     AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络,不花钱
     */
    static BOOL isOK = YES;
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                logdebug(@"网络不可用");
                isOK = NO;
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                logdebug(@"WIFI");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                logdebug(@"蜂窝网络");
                break;
            case AFNetworkReachabilityStatusUnknown:
                logdebug(@"哎呀，没见过");
                break;
            default:
                break;
        }
    }];
    return isOK;
}

@end
